package com.ruoyi.match.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.match.domain.vo.NoticeConfirmVo;
import com.ruoyi.match.mapper.MemberTeamMapper;
import com.ruoyi.match.mapper.NoticeMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.match.mapper.NoticeConfirmMapper;
import com.ruoyi.match.domain.NoticeConfirm;
import com.ruoyi.match.service.INoticeConfirmService;

/**
 * 公告确认Service业务层处理
 * 
 * @author zhangrui
 * @date 2025-04-05
 */
@Service
public class NoticeConfirmServiceImpl implements INoticeConfirmService 
{
    @Autowired
    private NoticeConfirmMapper noticeConfirmMapper;

    @Autowired
    private MemberTeamMapper memberTeamMapper;

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 查询公告确认
     * 
     * @param noticeId 公告确认主键
     * @return 公告确认
     */
    @Override
    public NoticeConfirm selectNoticeConfirmByNoticeId(Long noticeId)
    {
        return noticeConfirmMapper.selectNoticeConfirmByNoticeId(noticeId);
    }

    /**
     * 查询公告确认列表
     * 
     * @param noticeConfirm 公告确认
     * @return 公告确认
     */
    @Override
    public List<NoticeConfirm> selectNoticeConfirmList(NoticeConfirm noticeConfirm)
    {
        return noticeConfirmMapper.selectNoticeConfirmList(noticeConfirm);
    }

    /**
     * 新增公告确认
     * 
     * @param noticeConfirm 公告确认
     * @return 结果
     */
    @Override
    public int insertNoticeConfirm(NoticeConfirm noticeConfirm)
    {
        return noticeConfirmMapper.insertNoticeConfirm(noticeConfirm);
    }

    /**
     * 修改公告确认
     * 
     * @param noticeConfirm 公告确认
     * @return 结果
     */
    @Override
    public int updateNoticeConfirm(NoticeConfirm noticeConfirm)
    {
        return noticeConfirmMapper.updateNoticeConfirm(noticeConfirm);
    }

    /**
     * 批量删除公告确认
     * 
     * @param noticeIds 需要删除的公告确认主键
     * @return 结果
     */
    @Override
    public int deleteNoticeConfirmByNoticeIds(Long[] noticeIds)
    {
        return noticeConfirmMapper.deleteNoticeConfirmByNoticeIds(noticeIds);
    }

    /**
     * 删除公告确认信息
     * 
     * @param noticeId 公告确认主键
     * @return 结果
     */
    @Override
    public int deleteNoticeConfirmByNoticeId(Long noticeId)
    {
        return noticeConfirmMapper.deleteNoticeConfirmByNoticeId(noticeId);
    }

    //查询所有该用户确认过的公告id
    @Override
    public List<Long> findConfirmedNotices(List<Long> noticeIds, Long userId) {
//        做非空判断
        if (CollectionUtils.isEmpty(noticeIds)) {
            return Collections.emptyList();
        }
        return noticeConfirmMapper.findConfirmedNotices(noticeIds, userId);
    }

    //查询已确认过公告的队员名单，是否全部成员已确认
    @Override
    public NoticeConfirmVo selectConfirmedNames(Long noticeId) {
        NoticeConfirmVo noticeConfirmVo = new NoticeConfirmVo();
        //查询出所有确认者的名单
        noticeConfirmVo.setUserNames(noticeConfirmMapper.selectConfirmedNames(noticeId));
        if (noticeConfirmVo.getUserNames() == null){
            noticeConfirmVo.setAllConfirmed(false);

            return noticeConfirmVo;
        }

        //查询是否全部已读
//        根据公告id查询已确认队员数量
        int confirmedCount = noticeConfirmMapper.selectConfirmedCount(noticeId);
//        查询所有队员数量
        int memberCount = memberTeamMapper.selectMemberCountByTeamId(noticeMapper.findTeamIdByNoticeId(noticeId));
        if (confirmedCount == memberCount){
            noticeConfirmVo.setAllConfirmed(true);
        }

        return noticeConfirmVo;
    }


}
